package com.bbex.service.impl;

import com.bbex.constant.RedisConstants;
import com.bbex.entity.SourceAddress;
import com.bbex.mapper.SourceAddressDao;
import com.bbex.redis.RedisCacheManager;
import com.bbex.service.SourceAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by apa7 on 2019\4\25 0025.
 */
@Service
public class SourceAddressServiceImpl implements SourceAddressService {

    @Autowired
    private SourceAddressDao sourceAddressDao;

    @Autowired
    private RedisCacheManager redisCacheManager;

    @Override
    public SourceAddress findById(String id) {
        return sourceAddressDao.findById(id);
    }

    @Override
    public long countByAddress(String address) {
        return sourceAddressDao.count(address);
    }

    @Override
    public SourceAddress getLastest(String userId, String coinType){
        return sourceAddressDao.findLastest(userId, coinType);
    }

    @Override
    public boolean save(SourceAddress sourceAddress) {
        boolean result = sourceAddressDao.insert(sourceAddress) == 1;
        if (result) {
            //保存到TempAddrSet,作为临时待验证地址
            addTempAddress(sourceAddress.getCoinType(), sourceAddress.getAddress());
        }
        return result;
    }

    /**
     * 只提供给钱包服务调用
     *
     * @param sourceAddress
     * @return
     */
    @Override
    public boolean updateById(SourceAddress sourceAddress) {
        boolean result = sourceAddressDao.updateById(sourceAddress) == 1;
        return result;
    }

    @Override
    public SourceAddress findByUserIdAndCoinSymbol(String userId, String coinSymbol) {
        return sourceAddressDao.findByUserIdAndCoinSymbol(userId, coinSymbol);
    }

    @Override
    public List<SourceAddress> listByUserIdAndCoinType(String userId, String coinType) {
        return sourceAddressDao.listByUserIdAndCoinType(userId, coinType);
    }

    @Override
    public boolean deleteById(SourceAddress sourceAddress) {
        boolean result = sourceAddressDao.delete(sourceAddress.getId()) == 1;
        if (result) {
            //同时删除缓存中的地址
            redisCacheManager.getRedisTemplate().opsForSet().remove(RedisConstants.SOURCE_ADDRESS_SET_KEY + sourceAddress.getCoinType(), sourceAddress.getAddress());
        }
        return result;
    }

    @Override
    public Boolean isSourceAddress(String coinType, String address) {
        return redisCacheManager.getRedisTemplate().opsForSet().isMember(RedisConstants.SOURCE_ADDRESS_SET_KEY + coinType, address);
    }

    @Override
    public void addSourceAddress(String coinType, String address) {
        redisCacheManager.getRedisTemplate().opsForSet().add(RedisConstants.SOURCE_ADDRESS_SET_KEY + coinType, address);
    }

    @Override
    public Boolean isTempAddress(String coinType, String address) {
        return redisCacheManager.getRedisTemplate().opsForSet().isMember(RedisConstants.TEMP_SOURCE_ADDRESS_SET_KEY + coinType, address);
    }

    @Override
    public void addTempAddress(String coinType, String address) {
        redisCacheManager.getRedisTemplate().opsForSet().add(RedisConstants.TEMP_SOURCE_ADDRESS_SET_KEY + coinType, address);
    }

}